package com.molin;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //从请求头中拿参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        //验证失败，跳转到登录页面
        if(username==null||"".equals(username)){
            System.out.println(username+" "+password);
            resp.sendRedirect("/stu/login.jsp");
            return;
        }
        //将用户名密码放入会话域中
        HttpSession session = req.getSession();
        session.setAttribute("username",username);
        session.setAttribute("password",password);
        //验证成功跳转到首页，首页获取到数据后功能会不同
        resp.sendRedirect("/stu/index.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
